<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/8
 * Time: 上午11:37
 */

namespace app\index\controller;

use app\common\controller\Commoncontroller;
use app\common\library\UserAuth;
use think\Controller;
use think\Request;

class Basecontroller extends Commoncontroller
{

    /**
     * 返回码,默认为null,当设置了该值后将输出json数据
     * @var int
     */
    protected $code = null;

    /**
     * 返回内容,默认为null,当设置了该值后将输出json数据
     * @var mixed
     */
    protected $data = null;

    /**
     * 返回文本,默认为空
     * @var mixed
     */
    protected $msg = '';

    /**
     *
     * @var UserAuth
     */
    protected $user = null;
    /**
     * 无需登录的方法，默认全部都无需登录
     * @var array
     */
    protected $noNeedLogin = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $url = '';
        $modulename = $this->request->module();
        $controllername = strtolower($this->request->controller());
        $actionname = strtolower($this->request->action());

        $path = '/' . $modulename . '/' . str_replace('.', '/', $controllername) . '/' . $actionname;
        $this->user = UserAuth::instance();

        /* if (session('yxuid') == '') {
             $res = $this->loginWx();
         }*/
        $this->user->setRequestUri($path);

        if ($actionname != 'reg') {

            $this->user->init();
            // 检测是否需要验证登录
            if (!$this->user->match($this->noNeedLogin)) {

                $this->checkLogin();
            }
            $this->assign("user", $this->user);

        }


    }

    public function loginWx()
    {
        $openid = '';
        if (session('openid') != '') {
            $user = $this->user->where(array('openid' => session('openid')))->find();
            if ($user) {
                // session空值返回登录页

                session('yxuid', $user['id']);
                session('openid', $openid);

            } else {
                $this->getOpenid();

            }
        } else {
            $this->getOpenid();

        }


    }


    /**
     * @return 获取openid
     */
    public
    function getOpenid()
    {



        $openid = '';
        $request=Request::instance();
        if(!$request->get("code")){
            //以下信息可安放在用户登录界面上：
            $url= 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';

            header('Location:'.$url);//跳转到第三方登录入口
            exit;
        }

        //  https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx029ced8b97276573&redirect_uri=http://ccczuo.natapp1.cc/yigou/h5index/user/index&response_type=code&scope=snsapi_base&state=1#wechat_redirect
        $request = Request::instance();
        $code = $request->param('code');
        $appid = 'wxca265fcba0c858bb';
        $appsecret = '1b856580c4e46da5fc68b80d46718f47';
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid .
            "&secret=" . $appsecret . "&code=" . $code . "&grant_type=authorization_code";
        $res = https_request($url);
        $res = (json_decode($res, true));
        if (isset($res['openid'])) {
            $openid = $res['openid'];
            session('openid', $openid);

            $user = $this->user->where(array('openid' => $openid))->find();
            if ($user) {

                session('yxuid', $user['id']);
            }
        }

    }


    protected function checkLogin()
    {
        //检测是否登录
        if (!$this->user->isLogin()) {

            return $this->redirect(url('login/index'));

        }
    }
}
        